package com.cskaoyan.service.admin.keyword;

import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.MarketKeywordExample;
import com.cskaoyan.bean.bo.admin.keyword.KeywordListBo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Aaliyah
 * @date 2022/9/8 11:21
 * @description: 处理关键词模块的业务
 */
@Service
public class KeywordServiceImpl implements KeywordService {

    @Autowired
    MarketKeywordMapper marketKeywordMapper;


    /**
     * @description: 搜索符合条件的关键词数据并返回
     * @date: 2022/9/8 11:23
     * @param: [keywordListBo] 封装了搜索条件和分页信息
     */
    @Override
    public CommonData<MarketKeyword> keywordList(KeywordListBo keywordListBo) {


        // 设置查找条件
        MarketKeywordExample keywordExample = new MarketKeywordExample();
        keywordExample.setDistinct(true);
        keywordExample.setOrderByClause(keywordListBo.getSort()+" "+ keywordListBo.getOrder());
        MarketKeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (keywordListBo.getKeyword() != null) {
            criteria.andKeywordLike("%"+keywordListBo.getKeyword()+"%");
        }
        if(keywordListBo.getUrl()!=null){
            criteria.andUrlLike("%" + keywordListBo.getUrl() + "%");
        }

        // 分页查找
        PageHelper.startPage(keywordListBo.getPage(), keywordListBo.getLimit());
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(keywordExample);
        PageInfo<MarketKeyword> keywordPageInfo = new PageInfo<>(marketKeywords);

        // 返回查找结果
        return CommonData.data(keywordPageInfo);
    }

    /**
     * @description: 将新增的关键词数据写入数据库，并返回该条数据的其他信息
     * @date: 2022/9/8 12:15
     * @param: [marketKeyword] 封装了新数据信息
     */
    @Override
    public MarketKeyword keywordCreate(MarketKeyword marketKeyword) {

        // 补全时间信息
        Date date = new Date(System.currentTimeMillis());
        marketKeyword.setAddTime(date);
        marketKeyword.setUpdateTime(date);

        // 将数据放入数据库
        marketKeywordMapper.insertSelective(marketKeyword);

        // 查找该条数据的信息
        MarketKeywordExample keywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andKeywordEqualTo(marketKeyword.getKeyword());
        if (marketKeyword.getUrl() != null) {
            criteria.andUrlEqualTo(marketKeyword.getUrl());
        }
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(keywordExample);
        // 如果信息有多条，取最新一条
        int index = 0;
        if (marketKeywords.size() > 1) {
            index=marketKeywords.size()-1;
        }

        // 返回数据信息
        return marketKeywords.get(index);
    }

    /**
     * @description: 根据id，将更新后的关键词数据更新到数据库中，并修改更新时间
     * @date: 2022/9/8 14:47
     * @param: [marketKeyword] 封装了关键词id和更新后的关键词信息
     */
    @Override
    public MarketKeyword keywordUpdate(MarketKeyword marketKeyword) {

        // 不修改添加时间，将addTime置为null，并将update时间设置为现在
        marketKeyword.setAddTime(null);
        marketKeyword.setUpdateTime(new Date(System.currentTimeMillis()));

        marketKeywordMapper.updateByPrimaryKeySelective(marketKeyword);
        return marketKeywordMapper.selectByPrimaryKey(marketKeyword.getId());
    }

    /**
     * @description: 根据id对关键词数据进行逻辑删除，并修改更新时间
     * @date: 2022/9/8 14:56
     * @param: [marketKeyword] 需要删除的目标数据信息
     */
    @Override
    public void keywordDelete(MarketKeyword marketKeyword) {

        marketKeyword.setAddTime(null);
        marketKeyword.setUpdateTime(new Date(System.currentTimeMillis()));
        marketKeyword.setDeleted(true);

        marketKeywordMapper.updateByPrimaryKeySelective(marketKeyword);
    }
}
